//
//  BlueToothLib.m
//  BlueToothLeon
//
//  Created by apple on 14/11/3.
//  Copyright (c) 2014年 com.yourname. All rights reserved.
//

#import "BlueToothLib.h"

@implementation BlueToothLib


+(void)synchronizationTimeNew:(uint32_t)second withBlock:(BLECallBack)block
{
    set_local_time(second, block);
}


+(void)alarmGetWithBlock:(BLECallBack)block
{
    alarm_get(block);
}


+(void) alarmSetting:(alarm_setting_t *)alarm
          alarmCount:(uint8_t)alarmCount
           withBlock:(BLECallBack)block
{
    alarm_setting(alarm,alarmCount, block);
}

+(void) userinfoSetting:(user_info_t *)info withBlock:(BLECallBack)block

{
    userinfo_setting(info, block);
}

+(void) stepTargetSetting:(uint32_t)uSteps withBlock:(BLECallBack)block
{
    steptarget_setting(uSteps, block);
}

+(void)requestSportData:(uint8_t)request withBlock:(BLECallBack)block
{
    request_sport_data(request, block);
}

-(void)otaControlWithBlock:(BLECallBack)block
{
    ota_control(3, block);
}

-(void)forceToUpgradeWithBlock:(BLECallBack)block
{
    ota_control(1, block);
}

-(void)ifUpgradeWithBlock:(BLECallBack)block
{
    ota_control_2(2, 0, block);
}

+(void)getBatteryWithBlock:(BLECallBack)block
{
    get_device_battery(block);
}

-(void)otaSend:(uint8_t *)pBuffer
            en:(uint16_t)len withBlock:(BLECallBack)block
{
    ota_send(pBuffer,len, block);
}
-(void)otaSendOver:(uint16_t)crc
           fileLen:(uint32_t)file_len withBlock:(BLECallBack)block
{
    ota_send_over(crc,file_len, block);
}

+(BOOL)dataIsValid:(NSData *)data
{
    Byte *byteData = (Byte *)[data bytes];
    for (int i = 0; i < [data length]; i++)
    {
        if (byteData[i] != 0XFF)
        {
            return YES;
        }
    }
    return data.length == 0;
}

+(void)returnDataForCommand:(uint8_t)commandId
                        key:(uint8_t)key
                      value:(const uint8_t *)value
                     length:(uint16_t)length
{
    NSData * data = [NSData dataWithBytes:value length:length];
    switch (commandId) {
        case FIRMWARE_UPDATE_CMD_ID:
            
            break;
        case SET_CONFIG_COMMAND_ID:
        {
            [BlueToothLib handSettingCommandForKey:key data:data];
        }
            break;
        case HEALTH_DATA_COMMAND_ID:
        {
            [BlueToothLib handleSportCommandForKey:key data:data];
        }
            break;
        case BLUETOOTH_DEVICE_COMMAND_ID:
        {
            [BlueToothLib handleDeviceInfoCommandForKey:key data:data];
        }
            break;
        default:
            break;
    }
}

+(void)receivedResponse:(NSData *)data
{
    L1_receive_data((uint8_t *)data.bytes, data.length);
}

#pragma mark -

+ (void)handleSportCommandForKey:(uint8_t)key data:(NSData *)data
{
    switch (key) {
        case KEY_REQUEST_SPORT_DATA:
        {
            
        }
            break;
        case KEY_RETURN_CURRENT_TASK:
        {
            if ([BlueToothLib dataIsValid:data]) {
                [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFY_GET_CURRENT_TASK_DATA
                                                                    object:data];
            }
            [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFY_RECIVED_CURRENT_TASK_DATA
                                                                object:data];
        }
            break;
        case KEY_RETURN_CURRENT_SPORT:
        {
            if ([BlueToothLib dataIsValid:data]) {
                [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFY_GET_CURRENT_SPORT_DATA
                                                                    object:data];
            }
            [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFY_RECIVED_CURRENT_SPORT_DATA
                                                                object:data];
        }
            break;
        case KEY_HIS_DATA_SYNC_DATA:
        {
            if ([BlueToothLib dataIsValid:data]) {
                [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFY_GET_UNSYNC_SPORT_DATA
                                                                    object:data];
            }
            [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFY_RECIVED_UNSYNC_SPORT_DATA
                                                                object:data];
            
        }
            break;
        case KEY_SLEEP_DATA_SYNC_DATA:
        {
            if ([BlueToothLib dataIsValid:data]) {
                [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFY_GET_SLEEP_DATA
                                                                    object:data];
            }
            [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFY_RECIVED_SLEEP_DATA
                                                                object:data];
        }
            break;
        case KEY_NO_DATA_ANYMORE:
        {
            [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFY_NO_MORE_SPORT_DATA
                                                                object:data];
        }
            break;
            
        default:
            break;
    }
}

+(void)handSettingCommandForKey:(uint8_t)key data:(NSData *)data
{
    switch (key)
    {
        case KEY_USER_INFO_SETTING:
        {
        }
            break;
        case KEY_TIME_SETTINGS:
        {
        }
            break;
        case KEY_ALARM_SETTINGS:
        {
        }
            break;
        case KEY_ALARM_LIST_REQUEST:
        {
        }
            break;
        case KEY_ALARM_LIST_RETURN:
        {
            [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFY_GET_ALARMS
                                                                object:data];
        }
            break;
        case KEY_STEP_TARGET_SETTINGS:
        {
        }
            break;
        case KEY_PHONE_NOTIFY_SETTINGS:
        {
        }
            break;
    };
}

+(void)handleDeviceInfoCommandForKey:(uint8_t)key data:(NSData *)data
{
    switch (key)
    {
        case DEVICE_INFO_KEY_RETURN_BATTERY:
        {
            [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFY_GET_BATTERY
                                                                object:data];
            [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFY_RECIVED_BATTERY
                                                                object:data];
        }
            break;
    };
}

@end
